package com.bwie.auth.controller;

import com.bwie.auth.service.AuthService;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @className: AuthController
 * @Description: TODO
 * @version: v1.８.0
 * @author:
 * @date:
 */
@RestController
public class AuthController {
    @Autowired
    private AuthService authService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/login")
    public Result<String> login(@RequestBody User user){
        String token = authService.login(user);
        return Result.success(token);
    }

    @PostMapping("/sendMs")
    public Result sendMs(@RequestBody User user){
        authService.sendMs(user);
        return Result.success();
    }

    @GetMapping("/getLogin")
    public Result<User> getLogin(HttpServletRequest request){
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        User user = (User) redisTemplate.opsForValue().get(TokenConstants.TOKEN + userKey);
        return Result.success(user);
    }
}
